package model

import (
	"ginProject/biz/dao"
)

type ProductCategory struct {
	ID           int
	Name         string
	Code         string
	ParentId     int
	Level        int
	ModifiedTime string
	CreateTime   string
}

/**
 * @Author leibidong
 * @Description 查询出所有一级商品类目
 * @Date  2021/7/3 21:18
 **/
func QueryAllfirstCategory() []ProductCategory {
	var firstCategory []ProductCategory
	db := dao.InitDB()
	db.Raw("select * from product_category where parent_id=0 and level=1").Scan(&firstCategory)
	return firstCategory
}

/**
 * @Author leibidong
 * @Description 根据商品一级类目名称查询类目id
 * @Date  2021/7/3 22:08
 **/
func QueryByName(name string) ProductCategory {
	var productCategory ProductCategory
	db := dao.InitDB()
	db.Raw("select id from product_category where parent_id=0 and level=1 and name=?", name).Scan(&productCategory)
	return productCategory
}

/**
 * @Author leibidong
 * @Description 新增一级分类类别
 * @Date  2021/7/3 22:09
 **/
func InsertProductFirstName(category ProductCategory) int {
	db := dao.InitDB()
	db.Exec("insert into product_category(name,code,parent_id,level,modified_time,create_time) values"+
		"(?,?,?,?,?,?)", category.Name, category.Code, category.ParentId, category.Level, category.ModifiedTime, category.CreateTime)
	return 1
}

/**
 * @Author leibidong
 * @Description 查询最大的商品分类
 * @Date  2021/7/4 17:46
 **/
func SelectMaxId() ProductCategory {
	db := dao.InitDB()
	var productCategory ProductCategory
	db.Raw("select (id) from product_category ").Scan(&productCategory)
	return productCategory
}

/**
 * @Author leibidong
 * @Description 更新一级类目名字
 * @Date  2021/7/4 20:07
 **/
func UpdateName(id int, name string) {
	db := dao.InitDB()
	db.Exec("update product_category set name=? where id=? and parent_id=0 and level=1", name, id)
}

/**
 * @Author leibidong
 * @Description 删除一级类目商品分类
 * @Date  2021/7/4 20:32
 **/
func DeleteFirstCategory(id int) {
	db := dao.InitDB()
	db.Exec("delete from  product_category where  parent_id=0 and level=1 and id=?", id)
}

/**
 * @Author leibidong
 * @Description 查询所有二级分类
 * @Date  2021/7/4 21:27
 **/
func SelectSecondCategoryByName(name string) []ProductCategory {
	db := dao.InitDB()
	var productCategory []ProductCategory
	if len(name) == 0 {
		db.Raw("select * from product_category where parent_id!=0 and level=2").Scan(&productCategory)
		return productCategory
	} else {
		db.Raw("select * from product_category where level=2 and "+
			"parent_id =(SELECT id  from product_category where name=? "+
			"and parent_id=0 and level=1)", name).Scan(&productCategory)
		return productCategory
	}
}

/**
 * @Author leibidong
 * @Description 在以及类目下面添加二级类目名称
 * @Date  2021/7/4 21:31
 **/
func AddSecondProductCategory(category ProductCategory) {
	db := dao.InitDB()
	db.Exec("insert into product_category(name,code,parent_id,level,modified_time,create_time) values"+
		"(?,?,?,?,?,?)", category.Name, category.Code, category.ParentId, category.Level, category.ModifiedTime, category.CreateTime)
}

/**
 * @Author leibidong
 * @Description 修改二级类目名称
 * @Date  2021/7/4 21:29
 **/
func UpdateSecondProductCategory(id int, name string) {
	db := dao.InitDB()
	db.Exec("update product_category set name=? where id=? and parent_id!=0 and level=2", name, id)
}

/**
 * @Author leibidong
 * @Description 根据二级类目id查询一级类目名称
 * @Date  2021/7/5 11:28
 **/
func QueryFirstNameBySecondId(id int) ProductCategory {
	db := dao.InitDB()
	var pro ProductCategory
	db.Raw("select name from product_category where parent_id=0 and level=1 and id =(SELECT parent_id  from product_category where id=?  and level=2)", id).Scan(&pro)
	return pro
}

/**
 * @Author leibidong
 * @Description 根据id号删除对应二级类目
 * @Date  2021/7/5 11:49
 **/
func DeleteSecondCategory(id int) {
	db := dao.InitDB()
	db.Exec("delete from product_category where id=? and parent_id!=0 and level=2", id)
}

/**
 * @Author leibidong
 * @Description 根据商品id查询对应的以及分类
 * @Date  2021/7/6 18:41
 **/
func SelectFirstCategoryNameByProductId(id int) ProductCategory {
	db := dao.InitDB()
	var p ProductCategory
	db.Exec("select name from product_category where id=(select one_category_id"+
		" from product_info where customer_id is NULL and id=? ) and  parent_id=0 and level=1", id).Scan(&p)
	return p
}
